package com.monicash.monicash.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.monicash.monicash.R;
import com.monicash.monicash.base.BaseActivity;
import com.monicash.monicash.main.modle.PermisosModle;
import com.monicash.monicash.widge.DialogView;
import com.monicash.monicash.widge.HeaderViewLayout;
import com.monicash.monicash.widge.NewWebView;

import java.util.ArrayList;
import java.util.List;

public class PermisosActivity extends BaseActivity {

    HeaderViewLayout headerTitle;
    TextView mTvSms3;
    TextView mTvDiscrepar;
    TextView mTvConfirmar;
    LinearLayout mLlConfirmar;
    NewWebView webview;

    PermisosModle permisosModle = new PermisosModle();

    DialogView dialogView;
    public List<String> permissions = new ArrayList<>();



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permisos);
        init();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void initView() {
        headerTitle = findViewById(R.id.header_title);
        mTvSms3 = findViewById(R.id.text3);
        mTvDiscrepar = findViewById(R.id.tv_discrepar);
        mTvConfirmar = findViewById(R.id.tv_confirmar);
        mLlConfirmar = findViewById(R.id.ll_confirmar);
        webview = findViewById(R.id.webview);
        mTvSms3.setText("Deslice hacia abajo para leer todas las descripciones de permisos");
        mTvDiscrepar.setText("Discrepar");
        mTvConfirmar.setText("Confirmar");
        dialogView = new DialogView(this);

        dialogView.setDialogViewOnClickListener(new DialogView.DialogViewOnClickListener() {
            @Override
            public void onClickConfirm() {
                getPermissions();
            }

            @Override
            public void onClickCancel() {
                Intent intent = new Intent(PermisosActivity.this, PrivacyActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });
        mTvDiscrepar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogView.showToPermissionSettingDialog(PermisosActivity.this);
            }
        });

        mTvConfirmar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPermissions();
            }
        });
        webview.loadUrl("https://www.baidu.com/");
        webview.setOnNewWebViewScroolChangeListener(new NewWebView.ScrollInterface() {
            @Override
            public void onSChanged(int l, int t, int oldl, int oldt) {
                float webcontent = webview.getContentHeight() * webview.getScale();//webview的高度
                float webnow = webview.getHeight() + webview.getScrollY() + 1;//当前webview的高度
                if (webcontent == 0){
                    return;
                }
                if (webview.getContentHeight() * webview.getScale() - (webview.getHeight() + webview.getScrollY() + 1) == 0) {
                    //已经处于底端
                    mLlConfirmar.setVisibility(View.VISIBLE);
                } else {
                    mLlConfirmar.setVisibility(View.GONE);
                }
                //已经处于顶端
                if (webview.getScaleY() == 0) {
                    mLlConfirmar.setVisibility(View.GONE);
                }
            }
        });

        permissions.add(Permission.ACCESS_COARSE_LOCATION);
        permissions.add(Permission.READ_PHONE_STATE);
        permissions.add(Permission.CAMERA);
        setPermissionList(permissions);

    }
    @Override
    public void initData() {
        PermisosModle.Companion.getInstant().getWebLinkConfig();
    }


}
